﻿KISSY.add("fgt/sns/sina", function (S) {
    function Sina(config) {
        this._init(config || {});
    }

    S.augment(Sina, {
        _init: function (config) {
            var self = this;

            self.cnName = "新浪微博";
            self.spanClass = "weibo";
            self.aClass = "weiboaclass";
        },

        generateLink: function (snsModel) {
            var self = this,
                encode = encodeURIComponent;

            self.link = "http://service.weibo.com/share/share.php?appkey=1509587317&url=" + encode(snsModel.link) + "&title=" + encode(snsModel.title) + "&source=&content=utf-8";
            if (snsModel.pic) {
                self.link += "&pic=" + encode(snsModel.pic);
            }

            return self.link;
        }
    });

    return Sina;
});KISSY.add("fgt/sns/qq", function (S) {
    function QQ(config) {
        this._init(config || {});
    }

    S.augment(QQ, {
        _init: function (config) {
            var self = this;

            self.cnName = "腾讯微博";
            self.spanClass = "qq";
            self.aClass = "qqaclass";
        },

        generateLink: function (snsModel) {
            var self = this,
                encode = encodeURIComponent;

            self.link = [
                            'http://v.t.qq.com/share/share.php?source=100623044&url=',
                            encode(snsModel.link),
                            '&title=',
                            encode(snsModel.title),
                            '&site=',
                            encode('http://www.dahuoshuo.com'),
                            '&pic=',
                            encode(snsModel.pic)
                        ].join('');

            return self.link;
        }
    });

    return QQ;
});KISSY.add("fgt/sns", function (S, Sina, QQ, UI) {

    S.namespace("FGT.sns", true);

    S.mix(S.FGT.sns, {
        shareList: { sina: Sina/*, qq: QQ*/ },
        createBizShare: function (trigger, shareCfg) {
            var self = this,
                shareList = self.shareList,
                triggerNode,
                htmlTemplate = "<li><a href='{link}' class='{aclass}' alt='{name}' target='_blank'><span class='{spanclass}'>{name}</span></a></li>",
                content = "";

            if (S.isString(trigger)) {
                triggerNode = S.all(trigger);
            }

            if (!triggerNode) {
                S.log("请配置正确的trigger ID.", "error");
                return;
            }

            var shareConfig = CONFIG[shareCfg];

            if (!shareConfig) {
                S.log("请配置正确的SNS Biz Share CONFIG.", "error");
                return;
            }

            S.each(shareList, function (value, key, obj) {
                var snsObj = new value();
                content += S.substitute(htmlTemplate, {
                    link: snsObj.generateLink(shareConfig),
                    name: snsObj.cnName,
                    aclass: snsObj.aClass,
                    spanclass: snsObj.spanClass
                });
            });

            var snsSharePopupID = S.guid("fgt-popup"),
                snsSharePopupNode = S.one("<div id='" + snsSharePopupID + "' class='sns-share-popup fgt-overlay'>"
                                            + "<ul>" + content + "</ul>"
                                         + "</div>").appendTo("body");

            snsSharePopup = new UI.Popup("#" + snsSharePopupID, {
                trigger: triggerNode,
                showTriggerType: "mouseover",
                hideTriggerType: "mouseleave",
                prefixCls: "fgt-",
                alignNode: triggerNode,
                alignCfg: {
                    t: { points: ["tc", "bl"], offset: [-30, 0] },
                    b: { points: ["bc", "tl"], offset: [-30, 0] }
                }
            });
        }
    });

    return S.FGT.sns;

}, { requires: ["./sns/sina", "./sns/qq", "fgt/ui"] });